<?php
namespace app\controller\api;

use app\exception\ErrnoPlus;
use share\controller\ApiBaseController;
use share\exception\ApiException;
use share\exception\Errno;
use share\service\NotifyDingTalkService;
use share\utils\Helper;

class CommonController extends ApiBaseController{

    public function get_region_action()
    {
        $ip = $this->request->ip();
        if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) {
            throw new ApiException("the ip is error",ErrnoPlus::COMMON_ERROR);
        }
        $url = "https://api01.aliyun.venuscn.com/ip?ip=" . $ip;
        $headers = ["Authorization:APPCODE ced94ef7e43a419a9038bfc1210631fe"];
        $resp = Helper::requestGetJson($url,$headers);      
        if ($resp && $resp["data"] &&  $resp["data"]["country_id"]) {
            $country = $resp["data"]["country_id"];
            switch ($country) {
                case "CN":
                    $region = "ap-guangzhou-open";
                    break;
                case "JP":
                    $region = "ap-guangzhou-open";
                    break;
                case "DE":
                    $region = "eu-frankfurt";
                    break;
                case "SG":
                    $region = "ap-singapore";
                    break;
                default:
                    $region = "na-ashburn";
                    break;
            }
            return self::ok(["region" => $region]);
        }
        throw new ApiException("query region error",ErrnoPlus::COMMON_ERROR);
    }

    public function oauth_alarm_action(){
        // // TODO 这个接口直接发送短信 有安全漏洞 后期再处理 
        $res = NotifyDingTalkService::pushText("APP后台服务登录频繁异常，请立即检查相关服务是否正常工作");
        return self::ok(['res'=>$res]);
    }
}